package com.seven.commons.excel.easyexcel;

import com.alibaba.excel.context.AnalysisContext;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @工程名:common-utils
 * @author: sevenDay
 * @时间: 2019/1/4 17:00
 * @描述:
 */
@Slf4j
public class ReadExcelListener extends com.alibaba.excel.event.AnalysisEventListener {

   private List<Object> date  = Lists.newArrayList();

   private List<Integer> dateIndex = null;

    public ReadExcelListener(List<Integer> list){
        this.dateIndex = list;
    }

    @Override
    public void invoke(Object object, AnalysisContext context) {
        if (CollectionUtils.isEmpty(dateIndex)){
            date.add(object);
        }else{
            List<Object> list = (ArrayList<Object>)object;
            int indexSize = dateIndex.size();
            for (int j=0;j<indexSize;j++){
                int index = dateIndex.get(j);
                String str = ExcelDateUtils.getExcelNumberConverToDate(Double.parseDouble(list.get(index).toString()));
                list.remove(index);
                list.add(index,str);
                date.add(list);
                list = null;
            }
        }
        System.out.println(date);
    }
    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        dateIndex = null;
        System.out.println("finished .....");
    }
}
